package com.jaydenxiao.common.baserx;

import android.text.TextUtils;

import com.jaydenxiao.common.basebean.BaseRespose;
import com.jaydenxiao.common.basebean.BaseResposeUser;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * RxJava调度管理
 * Created by xsf
 * on 2016.08.14:50
 */
public class RxSchedulers {
    public static <T> Observable.Transformer io_main() {
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * 对服务器返回数据进行预处理
     *
     * @param <T>
     * @return
     */
    public static <T> Observable.Transformer<BaseRespose<T>, T> handleResult() {
        return new Observable.Transformer<BaseRespose<T>, T>() {
            @Override
            public Observable<T> call(Observable<BaseRespose<T>> tObservable) {
                return tObservable.map(new Func1<BaseRespose<T>, T>() {
                    @Override
                    public T call(BaseRespose<T> result) {
                        return result.getData();
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }    /**
     * 对服务器返回数据进行预处理
     *
     * @param <T>
     * @return
     */
    public static <T> Observable.Transformer<BaseResposeUser<T>, T> handleUserResult() {
        return new Observable.Transformer<BaseResposeUser<T>, T>() {
            @Override
            public Observable<T> call(Observable<BaseResposeUser<T>> tObservable) {
                return tObservable.map(new Func1<BaseResposeUser<T>, T>() {
                    @Override
                    public T call(BaseResposeUser<T> result) {
                        if (!TextUtils.isEmpty(result.getSessionid())) {
                        }
                        return result.getUser();
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }
//public static  <T>Observable.Transformer eventHandle(final Observable<T> observable) {
//    return new Observable.Transformer<Object,T>() {
//
//        @Override
//        public Observable call(Observable tObservable) {
//            return tObservable.flatMap(new Func1<Object, Observable<T>>() {
//                @Override
//                public Observable<T> call(Object result) {
//                    return observable;
//                }
//            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
//        }
//    };
//}
//    public static Observable.Transformer<BaseRespose, String> handleSuccessMsg() {
//        return new Observable.Transformer<BaseRespose, String>() {
//            @Override
//            public Observable<String> call(Observable<BaseRespose> tObservable) {
//                return tObservable.map(new Func1<BaseRespose, String>() {
//                    @Override
//                    public String call(BaseRespose result) {
//                        LogUtils.logd("result from api : " + result.toString());
//                        return result.getMsg();
//                    }
//                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
//            }
//        };
//    }

}
